package cn.me.alphamq.broker.config;

import cn.me.alphamq.broker.enumeration.RebalanceStrategyEnum;
import cn.me.alphamq.broker.util.QueueIdUtil;
import cn.me.alphamq.common.util.PropertiesUtil;

import java.util.Properties;

/**
 * broker 的配置类
 *
 * @author f
 */
public class BrokerProperties {

    /**
     * nameserver 的 ip 地址
     */
    private final String nameServerIp;

    /**
     * nameserver 的端口号
     */
    private final Integer nameServerPort;

    /**
     * 与 nameserver 建立连接时验证使用的用户名
     */
    private final String nameServerUsername;

    /**
     * 与 nameserver 建立连接时验证使用的密码
     */
    private final String nameServerPassword;

    /**
     * broker 的端口
     */
    private final Integer brokerPort;

    /**
     * 重分配的策略
     */
    private final RebalanceStrategyEnum rebalanceStrategy;

    /**
     * broker 的当前延迟队列 id
     */
    private final Integer brokerCurrDelayQueueId;

    /**
     * broker 的另一个延迟队列 id，与当前延迟队列 id 不一样
     */
    private final Integer brokerOtherDelayQueueId;

    /**
     * broker 是否正常关闭
     */
    private final Boolean brokerNormalClosure;

    public BrokerProperties(String nameServerIp, Integer nameServerPort, String nameServerUsername,
                            String nameServerPassword, Integer brokerPort, String rebalanceStrategy,
                            Integer brokerCurrDelayQueueId, Boolean brokerNormalClosure) {
        this.nameServerIp = nameServerIp;
        this.nameServerPort = nameServerPort;
        this.nameServerUsername = nameServerUsername;
        this.nameServerPassword = nameServerPassword;
        this.brokerPort = brokerPort;
        this.rebalanceStrategy = RebalanceStrategyEnum.of(rebalanceStrategy);
        this.brokerCurrDelayQueueId = brokerCurrDelayQueueId;
        this.brokerOtherDelayQueueId = QueueIdUtil.getOtherDelayQueueId(brokerCurrDelayQueueId);
        this.brokerNormalClosure = brokerNormalClosure;
    }

    /**
     * 根据 properties 加载一个全局配置的对象
     *
     * @param properties 配置对象
     * @return 填充配置的对象
     */
    public static BrokerProperties load(Properties properties) {
        return new BrokerProperties(
                PropertiesUtil.getNotNullProperty(properties, "nameserver.ip"),
                PropertiesUtil.getIntProperty(properties, "nameserver.port"),
                PropertiesUtil.getNotNullProperty(properties, "nameserver.username"),
                PropertiesUtil.getNotNullProperty(properties, "nameserver.password"),
                PropertiesUtil.getIntProperty(properties, "broker.port"),
                PropertiesUtil.getNotNullProperty(properties, "rebalance.strategy"),
                PropertiesUtil.getIntProperty(properties, "broker.delay-queue-id"),
                PropertiesUtil.getBooleanProperty(properties, "broker.normal-closure")
        );
    }

    /**
     * 修改配置文件中的延迟队列 id
     *
     * @param properties 配置对象
     */
    public void changeDelayQueueId(Properties properties) {
        properties.setProperty("broker.delay-queue-id", String.valueOf(brokerOtherDelayQueueId));
    }

    /**
     * 设置 broker 正常关闭
     *
     * @param properties 配置对象
     */
    public void setBrokerNormalClosure(Properties properties) {
        properties.setProperty("broker.normal-closure", String.valueOf(true));
    }

    /**
     * 设置 broker 尚未关闭
     *
     * @param properties 配置对象
     */
    public void setBrokerAbnormalClosure(Properties properties) {
        properties.setProperty("broker.normal-closure", String.valueOf(false));
    }

    public String getNameServerIp() {
        return nameServerIp;
    }

    public Integer getNameServerPort() {
        return nameServerPort;
    }

    public String getNameServerUsername() {
        return nameServerUsername;
    }

    public String getNameServerPassword() {
        return nameServerPassword;
    }

    public Integer getBrokerPort() {
        return brokerPort;
    }

    public RebalanceStrategyEnum getRebalanceStrategy() {
        return rebalanceStrategy;
    }

    public Integer getBrokerCurrDelayQueueId() {
        return brokerCurrDelayQueueId;
    }

    public Integer getBrokerOtherDelayQueueId() {
        return brokerOtherDelayQueueId;
    }

    public Boolean getBrokerNormalClosure() {
        return brokerNormalClosure;
    }
}
